-- Copyright (c) 2016 Lucky Byte, Inc.
--
-- Summary : 导入银联 ACOMA 对账数据文件
--
-- Author : xiaohu <xiaohu@lucky-byte.com>
-- Since  : 2016.2.3
--
-- Reference
--  006.3 - 中国银联银行卡系统技术规范 第三部分 文件接口规范
--
-- History
--  2016.02.03 : 建立此文件
--  2016.02.06 : 重命名 _dbload.lua -> _checkdb.lua
--  2016.02.20 : 增加部分调试日志
--  2016.07.29 : 文件拆分
--
local _load_file = require 'upay.dbload.loadfile'

-- 解析单行 ACOMA 数据
-- 以 table 返回解析后的数据，或 nil
--
local function _split_acoma_line(filepath, line_num, line, extra)
    if string.len(line) < 951 then
        logger.warn('ACOMA 文件[%s]第[%s]行数据[%s]长度[%d]不足[951].',
            filepath, line_num, line, string.len(line))
        return nil
    end
    local _year = os.date('%Y')
    if extra then
        _year = extra.year or os.date('%Y')
    end
    return {
        year         = _year,
        acqu_id      = string.trim(string.sub(line, 1, 11)),
        fowd_id      = string.trim(string.sub(line, 13, 23)),
        trace_num    = string.trim(string.sub(line, 25, 30)),
        trans_time   = string.trim(string.sub(line, 32, 41)),
        pri_acct     = string.trim(string.sub(line, 43, 61)),
        trans_amt    = string.trim(string.sub(line, 63, 74)),
        acpt_amt     = string.trim(string.sub(line, 76, 87)),
        fee          = string.trim(string.sub(line, 89, 100)),
        msg_type     = string.trim(string.sub(line, 102, 105)),
        proc_code    = string.trim(string.sub(line, 107, 112)),
        mer_type     = string.trim(string.sub(line, 114, 117)),
        term_id      = string.trim(string.sub(line, 119, 126)),
        acpt_id      = string.trim(string.sub(line, 128, 142)),
        retref_num   = string.trim(string.sub(line, 144, 155)),
        cond_code    = string.trim(string.sub(line, 157, 158)),
        auth_resp    = string.trim(string.sub(line, 160, 165)),
        recv_inst    = string.trim(string.sub(line, 167, 177)),
        old_trace    = string.trim(string.sub(line, 179, 184)),
        resp_code    = string.trim(string.sub(line, 186, 187)),
        entry_mode   = string.trim(string.sub(line, 189, 191)),
        recv_fee     = string.trim(string.sub(line, 193, 204)),
        recv_pay     = string.trim(string.sub(line, 206, 217)),
        tsfer_fee    = string.trim(string.sub(line, 219, 230)),
        odd_even     = string.trim(string.sub(line, 232, 232)),
        card_seqno   = string.trim(string.sub(line, 234, 236)),
        term_cab     = string.trim(string.sub(line, 238, 238)),
        ic_cond      = string.trim(string.sub(line, 240, 240)),
        old_datetime = string.trim(string.sub(line, 242, 251)),
        card_inst    = string.trim(string.sub(line, 253, 263)),
        trans_area   = string.trim(string.sub(line, 265, 265)),
        term_type    = string.trim(string.sub(line, 267, 268)),
        eci_flag     = string.trim(string.sub(line, 270, 271)),
        stage_fee    = string.trim(string.sub(line, 273, 284)),
        other        = string.trim(string.sub(line, 286, 299)),
        -- COMA 追加部分 (38 个字段)
        fowd_settl   = string.trim(string.sub(line, 301, 311)),
        acqu_settl   = string.trim(string.sub(line, 313, 323)),
        reverse      = string.trim(string.sub(line, 325, 325)),
        revoke       = string.trim(string.sub(line, 327, 327)),
        settl_date   = string.trim(string.sub(line, 329, 332)),
        settl_cnt    = string.trim(string.sub(line, 334, 335)),
        mer_name     = string.trim(gb18030.str(string.sub(line, 337, 376))),
        trans_curry  = string.trim(string.sub(line, 378, 380)),
        proxy_fee    = string.trim(string.sub(line, 382, 390)),
        mer_fee      = string.trim(string.sub(line, 392, 400)),
        mer_bank     = string.trim(string.sub(line, 402, 412)),
        mer_bank_fee = string.trim(string.sub(line, 414, 422)),
        role1        = string.trim(string.sub(line, 424, 434)),
        role1_fee    = string.trim(string.sub(line, 436, 444)),
        role2        = string.trim(string.sub(line, 446, 456)),
        role2_fee    = string.trim(string.sub(line, 458, 466)),
        role3        = string.trim(string.sub(line, 468, 478)),
        role3_fee    = string.trim(string.sub(line, 480, 488)),
        role4        = string.trim(string.sub(line, 490, 500)),
        role4_fee    = string.trim(string.sub(line, 502, 510)),
        role5        = string.trim(string.sub(line, 512, 522)),
        role5_fee    = string.trim(string.sub(line, 524, 532)),
        role6        = string.trim(string.sub(line, 534, 544)),
        role6_fee    = string.trim(string.sub(line, 546, 554)),
        role7        = string.trim(string.sub(line, 556, 566)),
        role7_fee    = string.trim(string.sub(line, 568, 576)),
        role8        = string.trim(string.sub(line, 578, 588)),
        role8_fee    = string.trim(string.sub(line, 590, 598)),
        role9        = string.trim(string.sub(line, 600, 610)),
        role9_fee    = string.trim(string.sub(line, 612, 620)),
        cust_fee1    = string.trim(string.sub(line, 622, 630)),
        cust_fee2    = string.trim(string.sub(line, 632, 640)),
        cust_fee3    = string.trim(string.sub(line, 642, 650)),
        in_cardno    = string.trim(string.sub(line, 652, 670)),
        stage_cnt    = string.trim(string.sub(line, 672, 673)),
        order_id     = string.trim(string.sub(line, 675, 714)),
        pay_type     = string.trim(string.sub(line, 716, 719)),
        reserve      = string.trim(string.sub(line, 721, 951)),
    }
end


-- 导入 ACOMA(机构一般交易流水文件记录格式) 文件内容到 upay_acoma 表中
--
local _load_acoma = function(filepath, year)
    return _load_file({
        filepath    = filepath,
        table_name  = 'upay_acoma',
        line_split  = _split_acoma_line,
        unique_keys = { 'trace_num', 'retref_num', 'settl_date' },
        extra       = { year = year },
    })
end


return _load_acoma
